<?php 

class Order_model extends CI_Model {
	
    var $_table = 'products_ordered';

    function getOrders($limit,$offset) {
        /*
        * Get list of orders
        */
        $sql = "select count(DISTINCT date_bought, customer_code, comment) as count from $this->_table order by date_bought DESC";
        $q = $this->db->query($sql);
        $tmp = $q->row();
        $res['totalRows'] = $tmp->count;
        $sql = "SELECT DISTINCT date_bought,customer_code,comment 
                FROM $this->_table 
                ORDER BY date_bought DESC
                LIMIT $offset,$limit";
        /*/*$sql = "SELECT DISTINCT date_bought, po.customer_code as c_code, comment, cus.name as c_name, address, email 
                        FROM $this->_table as po
                        JOIN customers as cus
                        ON cus.customer_code = po.customer_code
                        order by date_bought DESC LIMIT $offset,$limit";
        echo $sql;*/
        $q = $this->db->query($sql);

        $res['rows'] = $q->result();
        return $res;
    }
    
    function getOrderDetails($customer_code, $date_bought) {
        
        $where = array(
            'customer_code' => $customer_code,
            'date_bought'   => $date_bought
        );
        
        $q = $this->db->select()
                        ->from($this->_table)
                        ->where($where)
                        ->get();
        if($q->num_rows() > 0) {
            $res = $q->result();
            return $res;
        }
    }
    
    function getProduct($table, $id = NULL) {
        $q = $this->db  ->select()
                        ->from($table)
                        ->where('id', $id)
                        ->get();
        if($q->num_rows() > 0) {
            $res = $q->row();
            return $res;
        }
    }
    
    function getUser($customer_code) {
        $q = $this->db  ->select()
                        ->from('customers')
                        ->where('customer_code', $customer_code)
                        ->get();
        if($q->num_rows() > 0) {
            $res = $q->row();
            return $res;
        }
    }
    function updateStatusByID($id,$status) {
		$status = ($status == 1) ? 'yes' : 'no';
        $this->db->where('id_key',$id);
        $data = array('treated' => $status);

        $this->db->update($this->_table,$data);
        
    }
         
}